<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css" />
    <link rel="stylesheet" href="../../styles/common.css" />
    <link rel="stylesheet" href="../../styles/page.css" />
    <style>
        #member-app .notAdmin::after{
            border: 0 !important;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <div class="tableBar">
            <el-button
                    type="primary"
                    @click="addRoleHandle('add')"
            >
                + 添加角色
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                class="tableBox"
        >
            <el-table-column
                    prop="name"
                    label="角色名称"
            ></el-table-column>
            <el-table-column
                    prop="keyword"
                    label="关键字"
            ></el-table-column>
            <el-table-column
                    prop="description"
                    label="描述"
            ></el-table-column>
            <el-table-column
                    label="操作"
                    width="260"
                    align="center"
            >
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="addRoleHandle(scope.row.id)"
                    >
                        编辑
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="addClass(scope.row.id,scope.row.name)"
                    >
                        菜单配置
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="blueBug"
                            @click="addClassPermission(scope.row.id,scope.row.name)"
                    >
                        权限配置
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
    <!--菜单弹出框-->
    <el-dialog
            :title="classData.title"
            :visible.sync="classData.dialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
        >
            <el-form-item label="角色：">
                <span>{{name}}</span>
            </el-form-item>
            <el-form-item label="菜单：">
                <el-tree
                        :data="data"
                        show-checkbox
                        default-expand-all
                        node-key="id"
                        ref="tree"
                        highlight-current
                        :props="defaultProps">
                </el-tree>
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
<el-button
        size="medium"
        @click="classData.dialogVisible = false"
>取 消</el-button>
<el-button
        type="primary"
        size="medium"
        @click="submitForm()"
>确 定</el-button>
</span>
    </el-dialog>
    <!--权限设置弹出框 -->
    <el-dialog
            :title="classDataPermission.title"
            :visible.sync="classDataPermission.dialogVisible"
            width="30%"
            :before-close="handleClose"
    >
        <el-form
                class="demo-form-inline"
                label-width="100px"
        >
            <el-form-item label="角色：">
                <span>{{roleName}}</span>
            </el-form-item>
            <!-- 下拉框展示所有权限-->
            <el-form-item label="权限列表：">
                <el-select v-model="classDataPermission.value" multiple placeholder="请
选择">
                    <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span
                slot="footer"
                class="dialog-footer"
        >
<el-button
        size="medium"
        @click="classDataPermission.dialogVisible = false"
>取 消</el-button>
<el-button
        type="primary"
        size="medium"
        @click="submitFormPermission()"
>确 定</el-button>
</span>
    </el-dialog>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../plugins/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../api/role.js"></script>
<script src="../../api/menu.js"></script>
<script src="../../api/roleAndMenu.js"></script>
<script src="../../api/permission.js"></script>
<script src="../../api/roleAndPermission.js"></script>
<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 10,
                tableData : [],
                id : '',
                name: '',
                status : '',
                classData: {
                    'title': '菜单配置',
                    'dialogVisible': false,
                    'value':[]
                },
                options:[],
                classDataPermission: {
                    'title': '权限配置',
                    'dialogVisible': false,
                    'value':[]
                },
                roleName: '',
                roleId: '',
                data: [{
                    id: 1,
                    label: '一级 1',
                    children: [{
                        id: 4,
                        label: '二级 1-1',
                        children: [{
                            id: 9,
                            label: '三级 1-1-1'
                        }, {
                            id: 10,
                            label: '三级 1-1-2'
                        }]
                    }]
                }, {
                    id: 2,
                    label: '一级 2',
                    children: [{
                        id: 5,
                        label: '二级 2-1'
                    }, {
                        id: 6,
                        label: '二级 2-2'
                    }]
                }, {
                    id: 3,
                    label: '一级 3',
                    children: [{
                        id: 7,
                        label: '二级 3-1'
                    }, {
                        id: 8,
                        label: '二级 3-2'
                    }]
                }]
            }
        },
        computed: {},
        created() {
            this.init()
        },
        mounted() {
        },
        methods: {
//配置权限，保存中间表
            submitFormPermission(){
                const params = {
                    "roleid": this.roleId,
                    'perIds': this.classDataPermission.value
                }
                addRoleAndPermission(params).then(res => {
                    if (res.code === 1) {
                        this.$message.success('权限配置成功！')
                        this.classDataPermission.dialogVisible = false
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
                this.init();
            },
            submitForm(){
                const params = {
                    "roleid": this.id,
                    'menuids': this.$refs.tree.getCheckedKeys()
                }
                addRoleAndMenu(params).then(res => {
                    if (res.code === 1) {
                        this.$message.success('菜单配置成功！')
                        this.classData.dialogVisible = false
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                })
                this.init();
            },
            getCheckedNodes() {
                console.log(this.$refs.tree.getCheckedNodes());
            },
            getCheckedKeys() {
                console.log(this.$refs.tree.getCheckedKeys());
            },
            setCheckedNodes() {
                this.$refs.tree.setCheckedNodes([{
                    id: 5,
                    label: '二级 2-1'
                }, {
                    id: 9,
                    label: '三级 1-1-1'
                }]);
            },
            setCheckedKeys(data) {
                this.$refs.tree.setCheckedKeys(data);
            },
            resetChecked() {
                this.$refs.tree.setCheckedKeys([]);
            }
            ,
//打开权限配置弹窗
            addClassPermission(id,name) {
//查询数据库，展示弹框
                getPermissions().then(response=>{
                    if (response.code === 1) {
                        this.options=response.data;
                        this.classDataPermission.dialogVisible = true
                        this.roleId = id;
                        this.roleName =name;
//根据角色查询对应的菜单信息，回填回去
                        getPermissionsByRoleid(id).then(re=> {
                            if (re.code === 1) {
                                this.classDataPermission.value=re.data;
                            }
                        })
                    }else{
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            }
            ,
// 打开弹窗
            addClass(id,name) {
//查询数据库，展示弹框
                getMenus().then(res=>{
                    if (res.code === 1) {
                        this.data = res.data
                        this.classData.dialogVisible = true
                        this.id = id;
                        this.name =name;
//根据角色查询对应的菜单信息，回填回去
                        getMenusByRoleid(id).then(re=> {
                            if (re.code === 1) {
                                this.setCheckedKeys(re.data);
                            }
                        })
                    }else{
                        this.$message.error(res.msg || '操作失败')
                    }
                })
            }
            ,
            async init () {
                const params = {
                    page: this.page,
                    pageSize: this.pageSize
                }
                await getRoleList(params).then(res => {
                    if (String(res.code) === '1') {
                        this.tableData = res.data.records || []
                        this.counts = res.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
// 添加
            addRoleHandle (st) {
                if (st === 'add'){
                    window.parent.menuHandle({
                        id: '2',
                        url: '/backend/page/role/add.html',
                        name: '添加角色'
                    },true)
                } else {
                    window.parent.menuHandle({
                        id: '2',
                        url: '/backend/page/role/add.html?id='+st,
                        name: '修改角色'
                    },true)
                }
            },
// 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },
            handleSizeChange (val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange (val) {
                this.page = val
                this.init()
            }
        }
    })
</script>
</body>
</html>